package com.hnevc.mallserver3.controller;

import com.hnevc.mallserver3.common.ResponseResult;
import com.hnevc.mallserver3.entity.Address;
import com.hnevc.mallserver3.service.AddressService;
import com.hnevc.mallserver3.service.impl.AddressServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@Api(tags = "收获地址接口")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @PostMapping("/add")
    @ApiOperation(value = "addAddress", notes = "添加地址信息")
    public ResponseResult addAddress(@RequestBody Address address) {
        addressService.save(address);
        return ResponseResult.success(address);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getAddressById", notes = "根据ID获取地址信息")
    public ResponseResult getAddressById(@PathVariable Long id) {
        Address address = addressService.getById(id);
        return ResponseResult.success(address);
    }
    @GetMapping("/listbyuserid/{memberId}")
    @ApiOperation(value = "listAddresses", notes = "根据用户ID获取地址信息")
    public ResponseResult listAddresses(@PathVariable Long memberId) {
        List<Address> addresses = addressService.listByMemberId(memberId);
        return ResponseResult.success(addresses);
    }

    @GetMapping("/list")
    @ApiOperation(value = "listAddresses", notes = "获取所有地址信息")
    public ResponseResult listAddresses() {
        List<Address> addresses = addressService.list();
        return ResponseResult.success(addresses);
    }

    @PutMapping("/update")
    @ApiOperation(value = "updateAddress", notes = "更新地址信息")
    public ResponseResult updateAddress(@RequestBody Address address) {
        addressService.updateById(address);
        return ResponseResult.success(null);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "deleteAddress", notes = "根据ID删除地址信息")
    public ResponseResult deleteAddress(@PathVariable Long id) {
        addressService.removeById(id);
        return ResponseResult.success(null);
    }
}